% fname: routing_xy__topology_8x8__.m
% ./noxim -routing xy -dimx 8 -dimy 8  -sim 10000 -warmup 2000 -size 8 8 -buffer 4 -hs 27 0.2 -hs 28 0.2 -hs 35 0.2 -hs 36 0.2 

function [max_pir, max_throughput, min_delay] = routing_xy__topology_8x8__(symbol)

data = [
%             pir      avg_delay     throughput      max_delay       rpackets         rflits
            0.002        18.6102      0.0174286            105           1098           8784
            0.002        17.8919      0.0166783            120           1018           8139
            0.002        16.4765      0.0158512             94            999           7989
            0.002        17.5939      0.0169073            131            980           7845
            0.002        17.5474      0.0157891             91           1012           8084
            0.002        17.9577      0.0165271            139            993           7933
            0.002        18.3368       0.015957            131            974           7787
            0.002        17.3376      0.0158477             99           1013           8114
            0.002        17.1463      0.0167839            121            991           7922
            0.002        17.4331      0.0171373             78           1046           8363
            0.002        17.4057      0.0156488            125            986           7887
            0.002        17.7763      0.0171167            119           1028           8216
            0.002         18.148      0.0165184            124           1007           8061
            0.002        17.9777      0.0166129            102           1032           8240
            0.002        17.0994        0.01525            130            976           7808
            0.002        17.8131      0.0162361            106           1022           8183
            0.002        18.2144       0.016623            106           1031           8245
            0.002        17.8562      0.0170292            113           1022           8174
            0.002        17.7301      0.0155873             89            982           7856
            0.002        18.8197      0.0168952             96           1048           8380
           0.0022         19.016      0.0180927            115           1122           8974
           0.0022        22.1424      0.0185645            182           1152           9208
           0.0022        20.1912      0.0189375            121           1135           9090
           0.0022        19.8973      0.0190176             97           1217           9737
           0.0022        19.5522      0.0171845            145           1083           8661
           0.0022        20.8766      0.0186528            197           1175           9401
           0.0022        21.1204      0.0182399            169           1130           9047
           0.0022        19.0971      0.0173452            110           1092           8742
           0.0022        20.0404      0.0178223            188           1140           9125
           0.0022        18.9523      0.0173056            132           1091           8722
           0.0022         19.057      0.0178234            114           1123           8983
           0.0022         19.441      0.0179093            138           1111           8883
           0.0022        16.9142      0.0171048             97           1060           8484
           0.0022         19.045      0.0179782            119           1133           9061
           0.0022        19.4745      0.0180664            221           1157           9250
           0.0022        20.4398       0.018791            160           1146           9170
           0.0022        18.8957      0.0179324            102           1093           8751
           0.0022        18.2054      0.0173589            144           1076           8610
           0.0022        19.7586      0.0173418            119           1110           8879
           0.0022        19.7466      0.0171523            107           1097           8782
           0.0024        21.3961      0.0191855            224           1189           9516
           0.0024        22.0223      0.0196113            172           1256          10041
           0.0024        22.0556      0.0191211            178           1224           9790
           0.0024        23.0391      0.0209406            150           1279          10219
           0.0024        21.5738      0.0196055            152           1253          10038
           0.0024        21.3588      0.0191797            170           1229           9820
           0.0024         20.789      0.0191032            135           1204           9628
           0.0024        23.8691       0.020918            279           1276          10208
           0.0024        20.0964      0.0192754            118           1235           9869
           0.0024         22.607      0.0179551            238           1150           9193
           0.0024        23.3734      0.0207641            231           1288          10299
           0.0024        19.7238      0.0193669            108           1202           9606
           0.0024        21.2336      0.0191151            355           1203           9634
           0.0024        21.7562      0.0200645            182           1243           9952
           0.0024        20.2688      0.0189004             97           1209           9677
           0.0024        19.8237      0.0190242            146           1180           9436
           0.0024        21.0117      0.0189286            168           1192           9540
           0.0024        21.7326      0.0193281            146           1238           9896
           0.0024        24.0176      0.0202036            299           1253          10021
           0.0024        19.5961      0.0190302            136           1181           9439
           0.0026        23.5828      0.0206984            149           1304          10432
           0.0026          23.27       0.020666            204           1322          10581
           0.0026        28.4926      0.0212637            280           1360          10887
           0.0026        27.4978      0.0215655            200           1358          10869
           0.0026        20.9788      0.0206191            109           1319          10557
           0.0026        24.4567      0.0210714            511           1327          10620
           0.0026        32.6983      0.0216777            459           1389          11099
           0.0026        25.9222      0.0207956            275           1311          10481
           0.0026        29.0884      0.0213887            273           1369          10951
           0.0026        22.5584      0.0204434            211           1309          10467
           0.0026        25.6292      0.0208535            247           1335          10677
           0.0026        23.8796      0.0209742            231           1321          10571
           0.0026        25.2128       0.021125            172           1330          10647
           0.0026        27.6178      0.0210258            223           1324          10597
           0.0026        23.8562      0.0208418            202           1335          10671
           0.0026        21.0103      0.0200675            144           1264          10114
           0.0026        22.7548      0.0208044            181           1289          10319
           0.0026        23.8519      0.0198373            250           1249           9998
           0.0026        24.9399      0.0215195            222           1380          11018
           0.0026        24.8487      0.0217163            212           1368          10945
           0.0028        34.5071       0.023123            779           1479          11839
           0.0028         31.089       0.022623            352           1449          11583
           0.0028        32.6696      0.0235298            408           1483          11859
           0.0028        30.5736      0.0214551            388           1372          10985
           0.0028        33.6565      0.0230215            657           1473          11787
           0.0028        26.4553       0.021621            309           1364          10897
           0.0028        31.5878      0.0238242            340           1526          12198
           0.0028        33.9166      0.0220918            461           1415          11311
           0.0028        26.1572      0.0227695            202           1457          11658
           0.0028        31.2766      0.0220352            430           1410          11282
           0.0028         26.542       0.022459            290           1439          11499
           0.0028        26.0381      0.0221071            232           1392          11142
           0.0028        34.1236      0.0236484            699           1513          12108
           0.0028        24.7435      0.0223516            152           1431          11444
           0.0028        29.4641      0.0217754            446           1394          11149
           0.0028        27.8468      0.0232383            231           1488          11898
           0.0028        26.6255      0.0220312            207           1410          11280
           0.0028         33.542       0.022502            441           1439          11521
           0.0028        24.8495      0.0220879            213           1415          11309
           0.0028        28.3525      0.0226627            296           1427          11422
            0.003        32.3121      0.0247773            351           1586          12686
            0.003        33.3979      0.0245714            289           1548          12384
            0.003        33.3019      0.0244544            479           1540          12325
            0.003        47.3026      0.0243789            717           1560          12482
            0.003        36.9363       0.025002            350           1601          12801
            0.003        37.2279       0.024752            593           1558          12475
            0.003        36.1932      0.0248379            399           1589          12717
            0.003        31.7283      0.0239414            383           1531          12258
            0.003        32.2664      0.0235195            492           1505          12042
            0.003        32.9211      0.0243535            256           1559          12469
            0.003        28.9913      0.0239516            189           1487          11880
            0.003        27.1745      0.0242852            343           1553          12434
            0.003        29.8812      0.0240595            284           1515          12126
            0.003        38.2077      0.0243652            349           1560          12475
            0.003        28.4256      0.0238301            207           1525          12201
            0.003         27.363      0.0228262            242           1460          11687
            0.003        40.2011      0.0244609            510           1566          12524
            0.003        33.0463      0.0249043            246           1597          12751
            0.003        36.9601      0.0238555            358           1527          12214
            0.003        31.5416      0.0241973            344           1549          12389
           0.0032        57.5293      0.0255996           1479           1640          13107
           0.0032        32.4485      0.0245762            317           1574          12583
           0.0032        46.5239      0.0252051            656           1613          12905
           0.0032        33.6009      0.0251855            344           1611          12895
           0.0032        31.9085      0.0249277            233           1596          12763
           0.0032        40.2461      0.0254702            579           1605          12837
           0.0032         47.172       0.026082            496           1669          13354
           0.0032        64.3578      0.0263065           1595           1632          13048
           0.0032        49.9383      0.0260547            748           1669          13340
           0.0032         37.958      0.0264425            318           1668          13327
           0.0032        32.5955      0.0256895            369           1644          13153
           0.0032        64.1466      0.0247363           1007           1582          12665
           0.0032        34.1332      0.0252051            293           1614          12905
           0.0032        43.4382      0.0255449            526           1634          13079
           0.0032        42.3972      0.0256719            502           1644          13144
           0.0032        43.8124      0.0257344            756           1647          13176
           0.0032        53.1369      0.0260234            659           1666          13324
           0.0032        46.0794      0.0263398            849           1688          13486
           0.0032        33.5453      0.0259067            315           1632          13057
           0.0032        42.7663      0.0263512            442           1660          13281
           0.0034        75.6272      0.0276934           1802           1773          14179
           0.0034        80.5097      0.0275039           1946           1758          14082
           0.0034        40.2949       0.026166            481           1675          13397
           0.0034        42.2522      0.0267266            405           1709          13684
           0.0034        55.4497      0.0265605            765           1701          13599
           0.0034        49.7709      0.0268809            725           1720          13763
           0.0034        48.4002      0.0259316            491           1659          13277
           0.0034        45.3563      0.0267937            557           1687          13504
           0.0034         88.739      0.0277852           1446           1778          14226
           0.0034        87.9102      0.0273086           1595           1748          13982
           0.0034        49.0597      0.0272168            483           1741          13935
           0.0034        117.157      0.0277617           1843           1776          14214
           0.0034         64.546      0.0268203            981           1718          13732
           0.0034        62.1405      0.0266211            952           1701          13630
           0.0034        47.3923      0.0269258            865           1723          13786
           0.0034         82.306      0.0270625           1362           1732          13856
           0.0034         61.751      0.0275215           1059           1763          14091
           0.0034        53.7036      0.0276133           1199           1768          14138
           0.0034        54.4831      0.0268848            820           1720          13765
           0.0034        45.9471      0.0280703            373           1797          14372
           0.0036        138.289      0.0295496           1736           1862          14893
           0.0036        136.174      0.0278633           1786           1784          14266
           0.0036        241.584       0.029377           3479           1879          15041
           0.0036        214.515       0.029875           4942           1910          15296
           0.0036        95.1576      0.0279824           1221           1789          14327
           0.0036        64.3932      0.0285723            509           1831          14629
           0.0036        158.824      0.0287168           3325           1838          14703
           0.0036        90.7464      0.0281406           1684           1802          14408
           0.0036        110.444      0.0281973           2271           1805          14437
           0.0036        65.6035      0.0277773            668           1778          14222
           0.0036        155.872      0.0279609           3242           1790          14316
           0.0036        177.402      0.0292266           3440           1871          14964
           0.0036        118.432      0.0283438           3426           1814          14512
           0.0036        90.1903      0.0288281           1463           1844          14760
           0.0036        92.6393      0.0289219           1338           1852          14808
           0.0036        120.658      0.0284238           2110           1819          14553
           0.0036         92.088      0.0288066           1157           1841          14749
           0.0036        134.793      0.0292813           3660           1874          14992
           0.0036        191.391      0.0292852           3165           1873          14994
           0.0036        125.842      0.0290605           1753           1859          14879
           0.0038        264.209      0.0293867           4718           1879          15046
           0.0038         157.16      0.0282578           3257           1808          14468
           0.0038        327.533       0.030244           5339           1904          15243
           0.0038        236.199      0.0297773           4811           1906          15246
           0.0038        91.4814      0.0289512            907           1853          14823
           0.0038        152.669      0.0294785           2244           1886          15093
           0.0038        111.756      0.0297012           1587           1903          15207
           0.0038        90.3208      0.0302246           1184           1936          15475
           0.0038        180.647      0.0297793           4162           1905          15247
           0.0038        225.235      0.0288574           5245           1846          14775
           0.0038        327.775      0.0290527           6067           1858          14875
           0.0038        133.731      0.0298075           1899           1878          15023
           0.0038        149.756      0.0308066           2256           1969          15773
           0.0038        86.7391      0.0296895           1906           1901          15201
           0.0038        177.467      0.0290957           3489           1862          14897
           0.0038        183.127      0.0303359           5022           1941          15532
           0.0038        121.843       0.029248           2157           1872          14975
           0.0038        195.071      0.0297305           5776           1903          15222
           0.0038        326.906      0.0291348           5169           1864          14917
           0.0038        307.772      0.0292969           5543           1875          15000
];

rows = size(data, 1);
cols = size(data, 2);

data_delay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+1);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_delay = [data_delay; data(ifirst, 1:cols-5), avg ci];
end

figure(1);
hold on;
plot(data_delay(:,1), data_delay(:,2), symbol);

data_throughput = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+2);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_throughput = [data_throughput; data(ifirst, 1:cols-5), avg ci];
end

figure(2);
hold on;
plot(data_throughput(:,1), data_throughput(:,2), symbol);

data_maxdelay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+3);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_maxdelay = [data_maxdelay; data(ifirst, 1:cols-5), avg ci];
end

figure(3);
hold on;
plot(data_maxdelay(:,1), data_maxdelay(:,2), symbol);


%-------- Saturation Analysis -----------
slope=[];
for i=2:size(data_throughput,1),
    slope(i-1) = (data_throughput(i,2)-data_throughput(i-1,2))/(data_throughput(i,1)-data_throughput(i-1,1));
end

for i=2:size(slope,2),
    if slope(i) < (0.95*mean(slope(1:i)))
        max_pir = data_throughput(i, 1);
        max_throughput = data_throughput(i, 2);
        min_delay = data_delay(i, 2);
        break;
    end
end
